﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using Model;

namespace BLL
{
    /// <summary>
    /// BLL for book 
    /// </summary>
    public class Book
    {
        //private readonly int BOOKSTATE_ACTIVATE = 1;
        //private readonly int BOOKSTATE_DELETED = 0;

        private static readonly IBook bookDal = DALFactory.DataAccess.CreateBookDAL();

        /// <summary>
        /// Get book by its unique ISBN
        /// </summary>
        /// <param name="bookISBN"></param>
        /// <returns>If ISBN is found, return related book</returns>
        public BookInfo GetBook(string bookISBN)
        {

            BookInfo book = bookDal.SelectBooksByISBN(bookISBN);

            if (book != null)
            {
                return book;
            }
            else
            {
                throw new ApplicationException("No such book.");
            }
        }

        /// <summary>
        /// Get all books  
        /// </summary>
        /// <returns>Collection of BookInfo</returns>
        public IList<BookInfo> GetAllBooks()
        {
            // Not used
            // IList<BookInfo> books = bookDal.SelectAllBooks();
            // return books;
            IList<BookInfo> books = bookDal.SelectAllBooks();
            return books;
        }

        /// <summary>
        /// Search books by keyword
        /// </summary>
        /// <param name="keyword"></param>
        /// <returns>Collection of BookInfo</returns>
        public IList<BookInfo> SearchBooks(string keyword)
        {
            IList<BookInfo> books = bookDal.SelectBooksBySearch(keyword);
            return books;
        }

        /// <summary>
        /// Get new Books
        /// </summary>
        /// <returns>Collection of BookInfo</returns>
        public IList<BookInfo> GetNewBooks()
        {
            IList<BookInfo> books = bookDal.SelectNewBooks();
            return books;
        }
        
        /// <summary>
        ///  Get books that sale well
        /// </summary>
        /// <returns>Collection of BookInfo</returns>
        public IList<BookInfo> GetHotBooks()
        {
            IList<BookInfo> books = bookDal.SelectHotBooks();
            return books;
        }

        /// <summary>
        /// Add a book into shop
        /// </summary>
        /// <param name="book"></param>
        public void AddBook(string bookISBN,string bookName,string bookAuthor,string bookDesc,string bookPic,double bookPrice,int bookRemainNum,DateTime bookReleaseTime)
        {
            DateTime bookShelfTime = DateTime.Now;
            BookInfo book = new BookInfo(bookISBN, bookName, bookAuthor, bookDesc, bookPic, bookPrice, 0, bookRemainNum, bookShelfTime, bookReleaseTime);
            bookDal.InsertBook(book);
        }

        /// <summary>
        /// Modify information of a book
        /// </summary>
        /// <param name="book"></param>
        public void ModifyBook(string bookISBN, string bookName, string bookAuthor, string bookDesc, string bookPic, double bookPrice, int bookRemainNum, DateTime bookReleaseTime)
        {
            BookInfo book = bookDal.SelectBooksByISBN(bookISBN);
            book.BookName = bookName;
            book.BookAuthor = bookAuthor;
            book.BookDesc = bookDesc;
            book.BookPicture = bookPic;
            book.BookPrice = bookPrice;
            book.BookRemainNum = bookRemainNum;
            book.BookReleaseTime = bookReleaseTime;
            bookDal.UpdateBook(book);

        }

        /// <summary>
        /// Delete a book from shop
        /// </summary>
        /// <param name="bookISBN"></param>
        public void DisableBook(string bookISBN)
        {
            bookDal.DisableBook(bookISBN);
        }
        
    }
}
